/*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 ("License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.zimbra.com/license
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is: Zimbra AJAX Toolkit.
*
* The Initial Developer of the Original Code is Zimbra, Inc.
* Portions created by Zimbra are Copyright (C) 2005 Zimbra, Inc.
* All Rights Reserved.
*
* Portions of the original code have been adapted for CLDC 1.0 and MIDP 2.0 by CalemEAM Inc. 
* Portions created by CalemEAM are Copyright (C) 2008 CalemEAM Inc. All Rights Reserved.
*
* Contributor(s):
*
* ***** END LICENSE BLOCK *****
*/

package com.calemeam.text;

/**
 * Segment class
 * @author cli
 */
public class MicroSegment {
   protected MicroFormat format;
   protected String seg;
   protected int idx;
  
   public MicroSegment(MicroFormat format, String seg) {
      this.format=format;
      this.seg=seg;
   }

   //#if Debug
//#    public String toString() {
//#       return "segment: \""+this.seg+'"'; 
//#    }
   //#endif

   // Public methods
   public String format(Object o) {
      return this.seg;
   }
   
   //Idx set up
   public void setIdx(int idx) {
      this.idx=idx;
   }
   public int getIdx() {
      return this.idx;
   }
   
   //method for parsing
   //#if ParsingConfiguration
//#    public int parse(Object o, String s, int index) throws MicroParsingException {
//#       throw new MicroParsingException("Parsing not implemented for: "+s+", index="+index);
//#    }
   //#endif
   
   /**
    * Parses the string at the given index, initializes the parse object
    * (as appropriate), and returns the new index within the string for
    * the next parsing step.
    * <p>
    * <strong>Note:</strong>
    * This method must be implemented by sub-classes.
    *
    * @param o     [object] The parse object to be initialized.
    * @param s     [string] The input string to be parsed.
    * @param index [number] The index within the string to start parsing.
    */

   // Protected static methods
   //#if ParsingConfiguration
//#    public static int parseLiteral(String literal, String s, int index) throws MicroParsingException {
//#       if (s.length() - index < literal.length()) {
//#          throw new MicroParsingException("input too short"); // I18n
//#       }
//#       for (int i = 0; i < literal.length(); i++) {
//#          if (literal.charAt(i) != s.charAt(index + i)) {
//#             throw new MicroParsingException("input doesn't match"); // I18n
//#          }
//#       }
//#       return index + literal.length();
//#    };
   //#endif
   
   /**
    * Adding parsing functions to time data
    */
   //#if ParsingConfiguration
//#    public static int parseIntOpen(Object o, int fid, int adjust, String s, int index) throws MicroParsingException  {
//#       int len=s.length();
//#       int head = index;
//#       for (int i = head; i < len; i++) {
//#          if (!MicroFormatUtil.isDigit(s.charAt(index++))) {
//#             index--;
//#             break;
//#          }
//#       }
//#       int tail = index;
//#       if (head == tail) {
//#          throw new MicroParsingException("number not present, s="+s+", index="+index); // I18n
//#       }
//#       int v=Integer.parseInt(s.substring(head, tail));
//#       MicroFormatUtil.callObject(o, fid, v+adjust);
//#       return tail;
//#    };
   //#endif
   
   //#if ParsingConfiguration
//#    public static int parseLiteralOpen(String s, int index, char delimiter) throws MicroParsingException {
//#       int len=s.length();
//#       int head = index;
//#       for (int i = head; i < len; i++) {
//#          if (s.charAt(index++)==delimiter) {
//#             index--;
//#             break;
//#          }
//#       }
//# 
//#       int tail = index;
//#       if (head == tail) {
//#          throw new MicroParsingException("literal not present"); // I18n
//#       }
//#       return tail;
//#    };
   //#endif
   
   public MicroSegment() {
   }
   


   /**
    * Parses an integer at the offset of the given string and calls a
    * method on the specified object.
    *
    * @param adjust    [number]   The numeric adjustment to make on the
    *                             value before calling the object method.
    * @param s         [string]   The string to parse.
    * @param index     [number]   The index within the string to start parsing.
    * @param fixedlen  [number]   If specified, specifies the required number
    *                             of digits to be parsed.
    */
   //#if ParsingConfiguration
//#    public static int parseInt(Object o, int fid, int adjust, String s, int index, int fixedlen) throws MicroParsingException {
//#       int len = fixedlen> 0 ? fixedlen : s.length() - index;
//#       int head = index;
//#       for (int i = 0; i < len; i++) {
//#          int c=s.charAt(index++);
//#          if (!MicroFormatUtil.isDigit((char)c)) {//a digit
//#             index--;
//#             break;
//#          }
//#       }
//#       int tail = index;
//#       if (head == tail) {
//#          throw new MicroParsingException("number not present"); // I18n
//#       }
//#       if (fixedlen > 0  && tail - head != fixedlen) {
//#          throw new MicroParsingException("number too short"); // I18n
//#       }
//#       int value = Integer.parseInt(s.substring(head, tail));
//#       if (fid!=0) {
//#          MicroFormatUtil.callObject(o, fid, value+adjust);
//#       }
//#       return tail;
//#    }
   //#endif
}
